package com.github.ilioili.justdoit.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.FloatRange;
import android.util.AttributeSet;
import android.view.View;

import com.github.ilioili.justdoit.R;

/**
 * Created by User on 16/6/18.
 */
public class PieChartView extends View {
    private int size;
    private int cx;
    private int cy;
    private int bgColor = Color.parseColor("#55FFFFFF");
    private int fgColor = Color.parseColor("#FFFFFFFF");
    private float outerProgress = 0;
    private float innerProgress = 0;
    private int firstCircleRadius;//最外面大圆
    private int secondCircleRadius;
    private int thirdCircleRadius;
    private int firstCircleEdge;
    private int secondCircleEdge;
    private int thirdCircleEdge;
    private RectF rectF = new RectF();
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Path path = new Path();
    private int indicatorCircleRadius = 10;


    public PieChartView(Context context) {
        this(context, null);
    }

    public PieChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PieChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PieChartView, defStyleAttr, 0);
        indicatorCircleRadius = (int) typedArray.getDimension(R.styleable.PieChartView_pie_indicator_circle_radius, 10);
        firstCircleEdge = (int) typedArray.getDimension(R.styleable.PieChartView_pie_first_circle_from_edge, 20);
        secondCircleEdge = (int) typedArray.getDimension(R.styleable.PieChartView_pie_second_circle_from_edge, 50);
        thirdCircleEdge = (int) typedArray.getDimension(R.styleable.PieChartView_pie_third_circle_from_edge, 80);
        typedArray.recycle();
    }

    public float getOuterProgress() {
        return outerProgress;
    }

    public float getInnerProgress() {
        return innerProgress;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        size = width < height ? width : height;
        cx = cy = size / 2;
        firstCircleRadius = size / 2 - firstCircleEdge;
        secondCircleRadius = size / 2 - secondCircleEdge;
        thirdCircleRadius = size / 2 - thirdCircleEdge;
        rectF.set(cx - firstCircleRadius, cy - firstCircleRadius, cx + firstCircleRadius, cy + firstCircleRadius);
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.rotate(-90, cx, cy);
        drawOutCircle(canvas);
        drawInnerCirlce(canvas);
    }

    public void setOuterProgress(@FloatRange(from = 0, to = 1) float progress) {
        this.outerProgress = progress;
        postInvalidate();
    }

    public void setInnerProgress(@FloatRange(from = 0, to = 1) float progress) {
        this.innerProgress = progress;
        postInvalidate();
    }

    private void drawInnerCirlce(Canvas canvas) {
        canvas.save();
        paint.setStrokeWidth(1);
        for (int i = 0; i < 180; i++) {
            if (innerProgress * 180 < i) {
                paint.setColor(bgColor);
            } else {
                paint.setColor(fgColor);
            }
            canvas.drawLine(cx + thirdCircleRadius, cy, cx + secondCircleRadius, cy, paint);
            canvas.rotate((float) 2, cx, cy);
        }
        canvas.restore();
    }

    private void drawOutCircle(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(bgColor);
        canvas.drawCircle(cx, cy, firstCircleRadius, paint);
        paint.setColor(fgColor);
        path.rewind();
        path.moveTo(cx, size - firstCircleRadius);
        path.addArc(rectF, 0, outerProgress * 360);
        canvas.drawPath(path, paint);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.save();
        canvas.rotate(outerProgress * 360, cx, cy);

        canvas.drawCircle(cx + firstCircleRadius, cy, indicatorCircleRadius, paint);
        canvas.restore();
    }
}
